import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useUserStore } from './user'
import { fa } from 'element-plus/es/locale'

export const usePlayStore = defineStore('play', () => {

    /**
     *  设置变量
     */
    const keywords = ref<string>('')
    //当前播放的歌曲封面
    const cover = ref<string>('https://p1.music.126.net/T62FhTyq2Y-SY558NvME8g==/109951165498033031.jpg?param=200y110')
    // 播放列表
    const playList = ref<Array<musicItem>>([]);
    // 播放状态
    const playStatus = ref<boolean>(false);
    //当前播放的序号
    const currentMusicIndex = ref<number>(0);
    //当前的id
    const currentId = ref<any>(0);
    // 是否是来自本地缓存
    const isLocal = ref<boolean>(false);
    //总时长
    const totalTime = ref<number>(0)
    //当前时长
    const currentTime = ref<number>(0)
    //历史播放列表
    const historyPlaylist = ref<Array<musicItem>>([])
    watch(playList, newvalue => {
        if (playList.value.length > 0) {
            localStorage.setItem("playList", JSON.stringify(newvalue))
        }
    })
    watch(currentId, newvalue => {
        if (currentId.value) {
            localStorage.setItem("currentId", JSON.stringify(newvalue))
        }
    })
    watch(currentMusicIndex, newvalue => {
        localStorage.setItem("currentMusicIndex", JSON.stringify(newvalue))
    })
    onMounted(() => {
        if (localStorage.getItem("playList")) {
            playList.value = JSON.parse(window.localStorage.getItem("playList") as string)
            playStatus.value = true;
            isLocal.value = true;
        }
        if (localStorage.getItem("currentId")) {
            currentId.value = JSON.parse(window.localStorage.getItem("currentId") as string)
            playStatus.value = true;
            isLocal.value = true;
        }
        if (localStorage.getItem("currentMusicIndex")) {
            currentMusicIndex.value = JSON.parse(window.localStorage.getItem("currentMusicIndex") as string)
            playStatus.value = true;
            isLocal.value = true;
        }
    })
    /**
     * 
     * @param state pinia数据模型
     * @param playTime  播放时间
     */

    //设置播放时长
    function setTotalTime(time: number) {
        totalTime.value = time
    };
    //设置当前时长
    function setCurrentTime(time: number) {
        currentTime.value = time
    };
    //设置播放列表
    function setPlayList(state: any, musicList: any) {
        state.playList = musicList;
    };
    //设置播放状态
    function setPlayState(state: any, isPlay: boolean) {
        state.playStatus = isPlay;
    };
    //添加元素
    function pushMusic(item: musicItem) {
        playList.value.push(item);
    };
    //设置播放序号
    function setPlayIndex(state: any, Index: number) {
        state.currentMusicIndex = Index;
    }
    //设置当前播放的ID
    function setCurrentId(id: string) {
        currentId.value = id;
    }
    //设置添加到历史播放记录
    function pushInHistory(item: musicItem) {
        const state = useUserStore()
        if (state.login) {
            let count = 0;
            for (let i = 0; i < historyPlaylist.value.length; i++) {
                if (historyPlaylist.value[i].id == item.id) {
                    count++;
                    {
                        let template = historyPlaylist.value[i];
                        historyPlaylist.value.splice(i, 1);
                        historyPlaylist.value.unshift(template);
                    }
                }
            }
            if (count == 0) {
                historyPlaylist?.value.unshift(item);
            }
        }

    }
    //检测是否存在于播放列表
    function isExit(id: number) {
        for (let i = 0; i < playList.value.length; i++) {
            if (id === playList.value[i].id) {
                return false;
            }
        }
        return true;
    }
    //导出方法
    return { cover, isLocal, keywords, historyPlaylist, playList, playStatus, currentMusicIndex, currentId, totalTime, currentTime, isExit, pushInHistory, pushMusic, setCurrentTime, setTotalTime, setPlayList, setPlayState, setPlayIndex, setCurrentId }

})

interface playTime {
    total: number,
    current: number
}
interface musicItem {
    cover: string;
    name: string;
    id: number;
    album: string;
    dt: number;
    singer: string,
}